package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.commodity.models.vo.CommodityPriceVO;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgSectionCommodityRelationMapper;
import com.ztgf.shopping.models.bo.epg.EpgSectionCommodityRelationBO;
import com.ztgf.shopping.models.bo.epg.EpgSectionCommodityRelationSaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgSectionCommodityRelationDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgSectionCommodityRelationService;
import com.ztgf.shopping.models.vo.epg.EpgSectionCommodityRelationVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 频道栏目商品表服务
 *
 * @author YUE
 * @create 2020-05-18 11:26
 */
@DubboService
public class EpgSectionCommodityRelationServiceImpl extends ServiceImpl<EpgSectionCommodityRelationMapper, EpgSectionCommodityRelationDTO> implements IEpgSectionCommodityRelationService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgSectionCommodityRelationMapper epgSectionCommodityRelationMapper;

    @DubboReference
    private ICommodityInfoService iCommodityInfoService;

    /**
     * 根据频道id删除对应的商品
     * @param sectionId
     * @return
     */
    public boolean deleteBySectionId(Long sectionId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("section_id", sectionId);
        return delete(wrapper);
    }

    @Override
    public boolean add(EpgSectionCommodityRelationSaveBO epgSectionCommodityRelationSaveBO) {
        EpgSectionCommodityRelationDTO epgSectionCommodityRelationDTO = new EpgSectionCommodityRelationDTO();
        BeanUtils.copyProperties(epgSectionCommodityRelationSaveBO,epgSectionCommodityRelationDTO);
        Long id = ymlConfig.getPkId();
        epgSectionCommodityRelationDTO.setId(id);
        return this.insert(epgSectionCommodityRelationDTO);
    }

    /**
     * 添加必要数据
     * @param epgSectionCommodityRelationBO
     * @return
     */
    private EpgSectionCommodityRelationBO addMustData(EpgSectionCommodityRelationBO epgSectionCommodityRelationBO){
        epgSectionCommodityRelationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgSectionCommodityRelationBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgSectionCommodityRelationBO;
    }

    /**
     * 根据条件选择查询 频道栏目商品
     *
     * @param sectionId
     * @return
     */
    @Override
    public List<EpgSectionCommodityRelationVO> getListBySectionId(Long sectionId){
        EpgSectionCommodityRelationBO epgSectionCommodityRelationBO = new EpgSectionCommodityRelationBO();
        epgSectionCommodityRelationBO.setSectionId(sectionId);
        epgSectionCommodityRelationBO = addMustData(epgSectionCommodityRelationBO);
        Wrapper<EpgSectionCommodityRelationDTO> wrapper = getBySelectCommodityRelationDTO(epgSectionCommodityRelationBO);
        List<EpgSectionCommodityRelationDTO> epgSectionCommodityRelationDTOList = epgSectionCommodityRelationMapper.selectPage(new Page<>(0, ShoppingConstant.PLATFORM_SECTION_COMMODITY), wrapper);
        List<EpgSectionCommodityRelationVO> epgShopSectionCommodityRelationVOList = new ArrayList<>();
        for (EpgSectionCommodityRelationDTO epgSectionCommodityRelationDTO : epgSectionCommodityRelationDTOList) {
            EpgSectionCommodityRelationVO epgSectionCommodityRelationVO = new EpgSectionCommodityRelationVO();
            BeanUtils.copyProperties(epgSectionCommodityRelationDTO,epgSectionCommodityRelationVO);
            CommodityPriceVO commodityPriceVO = iCommodityInfoService.getByIdOtherInterface(epgSectionCommodityRelationDTO.getCommodityId());//商品
            epgSectionCommodityRelationVO.setCommodityPriceVO(commodityPriceVO);
            epgShopSectionCommodityRelationVOList.add(epgSectionCommodityRelationVO);
        }
        return epgShopSectionCommodityRelationVOList;
    }

    /**
     * 根据必要查询
     * @param epgSectionCommodityRelationBO
     * @return
     */
    private Wrapper<EpgSectionCommodityRelationDTO> getBySelectCommodityRelationDTO(EpgSectionCommodityRelationBO epgSectionCommodityRelationBO){
        Wrapper<EpgSectionCommodityRelationDTO> wrapper = new EntityWrapper<>();
        EpgSectionCommodityRelationDTO epgSectionCommodityRelationDTO = new EpgSectionCommodityRelationDTO();
        BeanUtils.copyProperties(epgSectionCommodityRelationBO,epgSectionCommodityRelationDTO);
        wrapper.eq("is_publish", epgSectionCommodityRelationBO.getIsPublish());
        wrapper.eq("section_id", epgSectionCommodityRelationBO.getSectionId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgSectionCommodityRelationBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }

    /**
     * 根据条件选择查询 频道栏目商品
     *
     * @param epgSectionCommodityRelationBO
     * @return
     */
    public List<EpgSectionCommodityRelationVO> getBySelectSectionCommodityRelationInfo(EpgSectionCommodityRelationBO epgSectionCommodityRelationBO){
        List<EpgSectionCommodityRelationVO> epgShopSectionCommodityRelationVOList = new ArrayList<>();
        Wrapper<EpgSectionCommodityRelationDTO> wrapper = new EntityWrapper<>();
        EpgSectionCommodityRelationDTO epgSectionCommodityRelationDTO = new EpgSectionCommodityRelationDTO();
        BeanUtils.copyProperties(epgSectionCommodityRelationBO,epgSectionCommodityRelationDTO);
        wrapper.eq("is_publish", epgSectionCommodityRelationBO.getIsPublish());
        wrapper.eq("section_id", epgSectionCommodityRelationBO.getSectionId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgSectionCommodityRelationBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);

        Page<EpgSectionCommodityRelationVO> page = new Page<>(epgSectionCommodityRelationBO.getPageNo(),epgSectionCommodityRelationBO.getPageSize());
        List<EpgSectionCommodityRelationDTO> epgShopSectionCommodityRelationDTOList = selectList(wrapper);
        for (EpgSectionCommodityRelationDTO sectionCommodityRelationDTO : epgShopSectionCommodityRelationDTOList) {
            CommodityPriceVO commodityPriceVO = iCommodityInfoService.getByIdOtherInterface(sectionCommodityRelationDTO.getCommodityId());//商品
            EpgSectionCommodityRelationVO epgSectionCommodityRelationVO = new EpgSectionCommodityRelationVO();
            BeanUtils.copyProperties(sectionCommodityRelationDTO,epgSectionCommodityRelationVO);
            epgSectionCommodityRelationVO.setCommodityPriceVO(commodityPriceVO);
            epgShopSectionCommodityRelationVOList.add(epgSectionCommodityRelationVO);
        }
        return epgShopSectionCommodityRelationVOList;
    }
}
